﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmSinglefeeMgr : Form
    {
        public List<ComboItem> collection = new List<ComboItem>();
        public FrmSinglefeeMgr()
        {
            InitializeComponent();
            InitializeComboSearch();

            comboBox_customer.SelectedIndex = 0;
            comboBox_genMonth.SelectedIndex = 0;
        }

        public FrmSinglefeeMgr(String customer, String genMonth)
        {
            InitializeComponent();
            InitializeComboSearch();

            comboBox_customer.SelectedItem = customer;
            comboBox_genMonth.SelectedItem = genMonth;
        }

        public void InitializeComboSearch()
        {
            collection.Add(new ComboItem { Text = "所有列", Value = "ALL" });
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                collection.Add(new ComboItem
                {
                    Text = dataGridView1.Columns[i].HeaderText,
                    Value = dataGridView1.Columns[i].DataPropertyName
                });
            }
            comboBox_cols.DisplayMember = "Text";
            comboBox_cols.ValueMember = "Value";
            comboBox_cols.DataSource = collection;
            comboBox_cols.SelectedValue = "ALL";


            comboBox_customer.Items.Add("<全部>");
            using (DataBase db = new DataBase())
            {
                string sql = "SELECT COMPANY_NAME FROM CJ_CUSTOMER_INFO order by COMPANY_NAME";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    comboBox_customer.Items.Add(dr[0].ToString());
                }
            }

            comboBox_genMonth.Items.Add("<全部>");
            using (DataBase db = new DataBase())
            {
                string sql = "SELECT DISTINCT GEN_MONTH FROM CJ_MAIN_FEE order by GEN_MONTH";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    comboBox_genMonth.Items.Add(dr[0].ToString());
                }
            }
        }

        private void FrmSinglefeeMgr_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            if (comboBox_customer.SelectedIndex != 0 || comboBox_genMonth.SelectedIndex != 0)
            {

                using (DataBase db = new DataBase())
                {
                    String sql = "SELECT * FROM View_MAIN_FEE WHERE 1=1 ";
                    if (comboBox_customer.SelectedIndex != 0)
                    {
                        sql += " AND COMPANY_NAME=@COMPANY_NAME ";
                        db.AddParameter("COMPANY_NAME", comboBox_customer.Text);
                    }
                    if (comboBox_genMonth.SelectedIndex != 0)
                    {
                        sql += " AND GEN_MONTH=@GEN_MONTH ";
                        db.AddParameter("GEN_MONTH", comboBox_genMonth.Text);
                    }

                    DataTable dt = db.ExecuteDataTable(sql);
                    LoadData(dt);
                }
            }
            else
            {
                this.viewMAINFEEBindingSource.DataSource = this.cjwyglDataSet_mainfee;
                this.viewMAINFEEBindingSource.DataMember = "View_MAIN_FEE";
                this.view_MAIN_FEETableAdapter.Fill(this.cjwyglDataSet_mainfee.View_MAIN_FEE);
            }
        }
        private void LoadData(DataTable dt)
        {
            this.viewMAINFEEBindingSource.DataSource = dt;
            this.view_MAIN_FEETableAdapter.Fill(this.cjwyglDataSet_mainfee.View_MAIN_FEE);
        }
        private void button_commonsearch_Click(object sender, EventArgs e)
        {
            string sql = "";
            string key = textBox_search.Text.Trim().Replace("'","''");
            if (key == "")
            {
                MessageBox.Show("请输入查询关键词");
                return;
            }
            if (comboBox_cols.SelectedValue.ToString() == "ALL")
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT * FROM View_MAIN_FEE WHERE 1=2 ");
                for (int i = 1; i < collection.Count; i++)
                {
                    sb.Append("or " + collection[i].Value + " like '%" + key + "%' ");
                }
                sql = sb.ToString();
            }
            else
            {
                sql = "SELECT * FROM View_MAIN_FEE WHERE " +
                    comboBox_cols.SelectedValue + " like '%" + key + "%' ";
            }
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                LoadData(dt);
            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            if (comboBox_customer.SelectedIndex != 0 || comboBox_genMonth.SelectedIndex != 0)
            {
                FrmSinglefeeUpdate2 frmAdd = new FrmSinglefeeUpdate2(comboBox_customer.Text, comboBox_genMonth.Text);
                frmAdd.ShowDialog();
            }
            else
            {
                FrmSinglefeeUpdate2 frmAdd = new FrmSinglefeeUpdate2();
                frmAdd.ShowDialog();
            }
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                FrmSinglefeeUpdate2 frmupdate = new FrmSinglefeeUpdate2(id);
                frmupdate.ShowDialog();

            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }

            DialogResult dr = MessageBox.Show("确定要删除吗？", "删除该条记录", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }


            int id = -1;
            int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
            if (id == -1)
            {
                MessageBox.Show("系统错误，未识别的行");
                return;
            }

            if (dataGridView1.SelectedRows[0].Cells[2].Value.ToString() == "房租")
            {
                // 删除客户在此费用年月上的所有单项费用
                using (DataBase db = new DataBase())
                {
                    db.AddParameter("ID", id);
                    DataTable dtMainFee = db.ExecuteDataTable("SELECT * FROM CJ_MAIN_FEE WHERE ID=@ID");
                    if (dtMainFee.Rows.Count != 1)
                    {
                        MessageBox.Show("系统错误，未找到要删除的数据");
                        return;
                    }

                    String COMPANY_ID = dtMainFee.Rows[0]["COMPANY_ID"].ToString();
                    String GEN_MONTH = dtMainFee.Rows[0]["GEN_MONTH"].ToString();


                    DialogResult dra = MessageBox.Show("删除“房租”费用记录将会删除此客户在 "+GEN_MONTH+" 月的所有单项费用记录，是否继续？", "删除多条记录", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (dra == DialogResult.No)
                    {
                        return;
                    }


                    db.AddParameter("COMPANY_ID", COMPANY_ID);
                    db.AddParameter("GEN_MONTH", GEN_MONTH);
                    int ret = db.ExecuteNonQuery("DELETE FROM CJ_MAIN_FEE WHERE COMPANY_ID = @COMPANY_ID AND GEN_MONTH = @GEN_MONTH");
                    MessageBox.Show("已删除" + ret + "行");
                    LoadData();
                }
            }
            else
            {
                // 删除一条单项费用记录
                using (DataBase db = new DataBase())
                {
                    string sql = "delete from CJ_MAIN_FEE where ID=@ID";
                    db.AddParameter("ID", id);
                    int ret = db.ExecuteNonQuery(sql);
                    MessageBox.Show("已删除" + ret + "行");
                    LoadData();
                }
            }
                

            
        }

    }
}
